<?php
namespace app\home\home;
class Pay extends Base {
    /**
     * 初始化方法
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 支付宝支付
     */
    public function alipay(){
        $order_id   = (int)input('order_id');
        $uid   = (int)input('id');
        self::checktoken($uid);
        $result     = db('home_orders')->field('o.id,o.ordersn,o.price,o.chargeid,o.status,c.name')->alias('o')->join('home_charge c','o.chargeid=c.id')->where(['o.id'=>$order_id])->find();
        $istest=1;
        if($istest == 1){
            $pay_result['total_amount']  = 0.01;
        }else{
            $pay_result['total_amount'] = $result['price'];
        }
        $pay_result['out_trade_no']   = $result['ordersn'];
        $pay_result['subject']        = $result['name'];

        $Alipayapi = new Alipayapi();
        return $Alipayapi->alipay($pay_result);
    }
    /**
     * 微信支付
     */
    public function wxpay(){
        $Wxpayapi = new Wxpayapi;
        $order_id   = (int)input('order_id');
        $result     = db('home_orders')->field('o.id,o.ordersn,o.price,o.chargeid,o.status,c.name')->alias('o')->join('home_charge c','o.chargeid=c.id')->where(['o.id'=>$order_id])->find();
        if ($result['price'] <= 0){
            return apistatus(0,'金额错误');
        }

        $istest=1;
        if($istest == 1){
            $pay_result['total_money']  =0.01;
        }else{
            $pay_result['total_money'] = $result['price'];
        }
        //微信支付
        $pay_result['title']    = $result['name'];
        $pay_result['order_sn'] = $result['ordersn'];
        $pay_result['chargeid'] = $result['chargeid'];
        $wx_img        = $Wxpayapi->getCode($pay_result);
        $res['wxpay']  = $wx_img;
        return apistatus(1,'成功',$res);
    }

    /**
     * 微信支付回调
     */
    public function payBack(){
        echo 1;die;
    }

    /**
     * 支付宝支付回调
     */
    public function alipayBack(){
        $data           = input();
//        file_put_contents('11.log',$data['out_trade_no']);
        db('home_order')->where(['ordersn'=>$data['out_trade_no']])->update(['status'=>2,'pay_time'=>time()]);
    }

}